package com.undsf.despot.actors.ng;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.undsf.despot.ResourceCache;
import com.undsf.utility.Table2D;

import java.util.List;

/**
 * Created by Arathi on 2016/10/14.
 */
public class Menu extends Actor {
    protected int menuWidth;
    protected int menuHeight;
    protected Table2D<MenuItem> menuItemTable;
    protected int menuItemWidth;
    protected int menuItemHeight;

    protected Pixmap bgImage;
    protected Texture bgTexture;

    public Menu(int width, int height) {
        menuWidth = width;
        menuHeight = height;

        bgImage = null;
    }

    public void setMenuTableSize(int x, int y) {
        menuItemTable = new Table2D<MenuItem>(x, y);
    }

    public void setMenuItemSize(int width, int height) {
        menuItemWidth = width;
        menuItemHeight = height;
    }

    public void addMenuItem(int x, int y, String label, String command) {
        MenuItem mItem = new MenuItem();
        mItem.label = label;
        mItem.cellX = x;
        mItem.cellY = y;
        mItem.command = command;

        menuItemTable.setData(x, y, mItem);
    }

    public void generateImage() {
        bgImage = new Pixmap(menuWidth, menuHeight, Pixmap.Format.RGB888);
        int rectOffsetX = -1;
        int rectOffsetY = -1;
        int pointOffsetX = 0;
        int pointOffsetY = 0;

        // 背景
        bgImage.setColor(new Color(0x1c1c1cdd));
        bgImage.fillRectangle(0, 0, menuWidth, menuHeight);

        // 边框
        bgImage.setColor(Color.WHITE);
        bgImage.drawRectangle(2+rectOffsetX, 3+rectOffsetY, menuWidth-2, menuHeight-4);
        bgImage.drawRectangle(3+rectOffsetX, 2+rectOffsetY, menuWidth-4, menuHeight-2);
        bgImage.drawPixel(3+pointOffsetX, 3+pointOffsetY);
        bgImage.drawPixel(3+pointOffsetX, menuHeight-4+pointOffsetY);
        bgImage.drawPixel(menuWidth-4+pointOffsetX, 3+pointOffsetY);
        bgImage.drawPixel(menuWidth-4+pointOffsetX, menuHeight-4+pointOffsetY);

        bgTexture = new Texture(bgImage);
    }

    public void drawText(Batch batch, float parentAlpha) {
        // 文本
        List<MenuItem> menuItems = menuItemTable.toLinearList();
        BitmapFont bmpFont12 = (BitmapFont) ResourceCache.GetInstance().getResourceByName("bmpFont12");
        for (MenuItem mItem : menuItems) {
            int offsetX = mItem.cellX * menuItemWidth;
            int offsetY = (menuItemTable.getHeight() - mItem.cellY) * menuItemHeight;
            bmpFont12.draw(batch, mItem.label, getX() + offsetX, getY() + offsetY);
        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        if (bgTexture == null) {
            generateImage();
        }
        batch.draw(bgTexture, getX(), getY());
        drawText(batch, parentAlpha);
    }
}
